<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - Lua 5.4.0-alpha - src/lstate.c</title>
  <link rel="stylesheet" type="text/css" href="../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../index.html">top level</a> - <a href="index.html">src</a> - lstate.c</td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">Lua 5.4.0-alpha</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">208</td>
            <td class="headerCovTableEntry">221</td>
            <td class="headerCovTableEntryHi">94.1 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2019-06-13 19:44:19</td>
            <td></td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<span class="lineNum">       1 </span>            : /*
<span class="lineNum">       2 </span>            : ** $Id: lstate.c $
<span class="lineNum">       3 </span>            : ** Global State
<span class="lineNum">       4 </span>            : ** See Copyright Notice in lua.h
<span class="lineNum">       5 </span>            : */
<span class="lineNum">       6 </span>            : 
<span class="lineNum">       7 </span>            : #define lstate_c
<span class="lineNum">       8 </span>            : #define LUA_CORE
<span class="lineNum">       9 </span>            : 
<span class="lineNum">      10 </span>            : #include &quot;lprefix.h&quot;
<span class="lineNum">      11 </span>            : 
<span class="lineNum">      12 </span>            : 
<span class="lineNum">      13 </span>            : #include &lt;stddef.h&gt;
<span class="lineNum">      14 </span>            : #include &lt;string.h&gt;
<span class="lineNum">      15 </span>            : 
<span class="lineNum">      16 </span>            : #include &quot;lua.h&quot;
<span class="lineNum">      17 </span>            : 
<span class="lineNum">      18 </span>            : #include &quot;lapi.h&quot;
<span class="lineNum">      19 </span>            : #include &quot;ldebug.h&quot;
<span class="lineNum">      20 </span>            : #include &quot;ldo.h&quot;
<span class="lineNum">      21 </span>            : #include &quot;lfunc.h&quot;
<span class="lineNum">      22 </span>            : #include &quot;lgc.h&quot;
<span class="lineNum">      23 </span>            : #include &quot;llex.h&quot;
<span class="lineNum">      24 </span>            : #include &quot;lmem.h&quot;
<span class="lineNum">      25 </span>            : #include &quot;lstate.h&quot;
<span class="lineNum">      26 </span>            : #include &quot;lstring.h&quot;
<span class="lineNum">      27 </span>            : #include &quot;ltable.h&quot;
<span class="lineNum">      28 </span>            : #include &quot;ltm.h&quot;
<span class="lineNum">      29 </span>            : 
<span class="lineNum">      30 </span>            : 
<span class="lineNum">      31 </span>            : 
<span class="lineNum">      32 </span>            : /*
<span class="lineNum">      33 </span>            : ** thread state + extra space
<span class="lineNum">      34 </span>            : */
<span class="lineNum">      35 </span>            : typedef struct LX {
<span class="lineNum">      36 </span>            :   lu_byte extra_[LUA_EXTRASPACE];
<span class="lineNum">      37 </span>            :   lua_State l;
<span class="lineNum">      38 </span>            : } LX;
<span class="lineNum">      39 </span>            : 
<span class="lineNum">      40 </span>            : 
<span class="lineNum">      41 </span>            : /*
<span class="lineNum">      42 </span>            : ** Main thread combines a thread state and the global state
<span class="lineNum">      43 </span>            : */
<span class="lineNum">      44 </span>            : typedef struct LG {
<span class="lineNum">      45 </span>            :   LX l;
<span class="lineNum">      46 </span>            :   global_State g;
<span class="lineNum">      47 </span>            : } LG;
<span class="lineNum">      48 </span>            : 
<span class="lineNum">      49 </span>            : 
<span class="lineNum">      50 </span>            : 
<span class="lineNum">      51 </span>            : #define fromstate(L)    (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
<span class="lineNum">      52 </span>            : 
<span class="lineNum">      53 </span>            : 
<span class="lineNum">      54 </span>            : /*
<span class="lineNum">      55 </span>            : ** A macro to create a &quot;random&quot; seed when a state is created;
<span class="lineNum">      56 </span>            : ** the seed is used to randomize string hashes.
<span class="lineNum">      57 </span>            : */
<span class="lineNum">      58 </span>            : #if !defined(luai_makeseed)
<span class="lineNum">      59 </span>            : 
<span class="lineNum">      60 </span>            : #include &lt;time.h&gt;
<span class="lineNum">      61 </span>            : 
<span class="lineNum">      62 </span>            : /*
<span class="lineNum">      63 </span>            : ** Compute an initial seed with some level of randomness.
<span class="lineNum">      64 </span>            : ** Rely on Address Space Layout Randomization (if present) and
<span class="lineNum">      65 </span>            : ** current time.
<span class="lineNum">      66 </span>            : */
<span class="lineNum">      67 </span>            : #define addbuff(b,p,e) \
<span class="lineNum">      68 </span>            :   { size_t t = cast_sizet(e); \
<span class="lineNum">      69 </span>            :     memcpy(b + p, &amp;t, sizeof(t)); p += sizeof(t); }
<span class="lineNum">      70 </span>            : 
<span class="lineNum">      71 </span><span class="lineCov">        108 : static unsigned int luai_makeseed (lua_State *L) {</span>
<span class="lineNum">      72 </span>            :   char buff[3 * sizeof(size_t)];
<span class="lineNum">      73 </span><span class="lineCov">        108 :   unsigned int h = cast_uint(time(NULL));</span>
<span class="lineNum">      74 </span><span class="lineCov">        108 :   int p = 0;</span>
<span class="lineNum">      75 </span><span class="lineCov">        108 :   addbuff(buff, p, L);  /* heap variable */</span>
<span class="lineNum">      76 </span><span class="lineCov">        108 :   addbuff(buff, p, &amp;h);  /* local variable */</span>
<span class="lineNum">      77 </span><span class="lineCov">        108 :   addbuff(buff, p, &amp;lua_newstate);  /* public function */</span>
<span class="lineNum">      78 </span>            :   lua_assert(p == sizeof(buff));
<span class="lineNum">      79 </span><span class="lineCov">        108 :   return luaS_hash(buff, p, h);</span>
<span class="lineNum">      80 </span>            : }
<span class="lineNum">      81 </span>            : 
<span class="lineNum">      82 </span>            : #endif
<span class="lineNum">      83 </span>            : 
<span class="lineNum">      84 </span>            : 
<span class="lineNum">      85 </span>            : /*
<span class="lineNum">      86 </span>            : ** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
<span class="lineNum">      87 </span>            : ** invariant (and avoiding underflows in 'totalbytes')
<span class="lineNum">      88 </span>            : */
<span class="lineNum">      89 </span><span class="lineCov">        529 : void luaE_setdebt (global_State *g, l_mem debt) {</span>
<span class="lineNum">      90 </span><span class="lineCov">        529 :   l_mem tb = gettotalbytes(g);</span>
<span class="lineNum">      91 </span>            :   lua_assert(tb &gt; 0);
<span class="lineNum">      92 </span><span class="lineCov">        529 :   if (debt &lt; tb - MAX_LMEM)</span>
<span class="lineNum">      93 </span><span class="lineNoCov">          0 :     debt = tb - MAX_LMEM;  /* will make 'totalbytes == MAX_LMEM' */</span>
<span class="lineNum">      94 </span><span class="lineCov">        529 :   g-&gt;totalbytes = tb - debt;</span>
<span class="lineNum">      95 </span><span class="lineCov">        529 :   g-&gt;GCdebt = debt;</span>
<span class="lineNum">      96 </span><span class="lineCov">        529 : }</span>
<span class="lineNum">      97 </span>            : 
<span class="lineNum">      98 </span>            : 
<span class="lineNum">      99 </span>            : /*
<span class="lineNum">     100 </span>            : ** Increment count of &quot;C calls&quot; and check for overflows. In case of
<span class="lineNum">     101 </span>            : ** a stack overflow, check appropriate error (&quot;regular&quot; overflow or
<span class="lineNum">     102 </span>            : ** overflow while handling stack overflow).
<span class="lineNum">     103 </span>            : ** If 'nCcalls' is larger than LUAI_MAXCSTACK but smaller than
<span class="lineNum">     104 </span>            : ** LUAI_MAXCSTACK + CSTACKCF (plus 2 to avoid by-one errors), it means
<span class="lineNum">     105 </span>            : ** it has just entered the &quot;overflow zone&quot;, so the function raises an
<span class="lineNum">     106 </span>            : ** overflow error.
<span class="lineNum">     107 </span>            : ** If 'nCcalls' is larger than LUAI_MAXCSTACK + CSTACKCF + 2
<span class="lineNum">     108 </span>            : ** (which means it is already handling an overflow) but smaller than
<span class="lineNum">     109 </span>            : ** 9/8 of LUAI_MAXCSTACK, does not report an error (to allow message
<span class="lineNum">     110 </span>            : ** handling to work).
<span class="lineNum">     111 </span>            : ** Otherwise, report a stack overflow while handling a stack overflow
<span class="lineNum">     112 </span>            : ** (probably caused by a repeating error in the message handling
<span class="lineNum">     113 </span>            : ** function).
<span class="lineNum">     114 </span>            : */
<span class="lineNum">     115 </span><span class="lineCov">      39114 : void luaE_enterCcall (lua_State *L) {</span>
<span class="lineNum">     116 </span><span class="lineCov">      39114 :   int ncalls = getCcalls(L);</span>
<span class="lineNum">     117 </span><span class="lineCov">      39114 :   L-&gt;nCcalls++;</span>
<span class="lineNum">     118 </span><span class="lineCov">      39114 :   if (ncalls &gt;= LUAI_MAXCSTACK) {  /* possible overflow? */</span>
<span class="lineNum">     119 </span><span class="lineNoCov">          0 :     luaE_freeCI(L);  /* release unused CIs */</span>
<span class="lineNum">     120 </span><span class="lineNoCov">          0 :     ncalls = getCcalls(L);  /* update call count */</span>
<span class="lineNum">     121 </span><span class="lineNoCov">          0 :     if (ncalls &gt;= LUAI_MAXCSTACK) {  /* still overflow? */</span>
<span class="lineNum">     122 </span><span class="lineNoCov">          0 :       if (ncalls &lt;= LUAI_MAXCSTACK + CSTACKCF + 2) {</span>
<span class="lineNum">     123 </span>            :         /* no error before increments; raise the error now */
<span class="lineNum">     124 </span><span class="lineNoCov">          0 :         L-&gt;nCcalls += (CSTACKCF + 4);  /* avoid raising it again */</span>
<span class="lineNum">     125 </span><span class="lineNoCov">          0 :         luaG_runerror(L, &quot;C stack overflow&quot;);</span>
<span class="lineNum">     126 </span>            :       }
<span class="lineNum">     127 </span><span class="lineNoCov">          0 :       else if (ncalls &gt;= (LUAI_MAXCSTACK + (LUAI_MAXCSTACK &gt;&gt; 3)))</span>
<span class="lineNum">     128 </span><span class="lineNoCov">          0 :         luaD_throw(L, LUA_ERRERR);  /* error while handling stack error */</span>
<span class="lineNum">     129 </span>            :     }
<span class="lineNum">     130 </span>            :   }
<span class="lineNum">     131 </span><span class="lineCov">      39114 : }</span>
<span class="lineNum">     132 </span>            : 
<span class="lineNum">     133 </span>            : 
<span class="lineNum">     134 </span><span class="lineCov">       1150 : CallInfo *luaE_extendCI (lua_State *L) {</span>
<span class="lineNum">     135 </span>            :   CallInfo *ci;
<span class="lineNum">     136 </span>            :   lua_assert(L-&gt;ci-&gt;next == NULL);
<span class="lineNum">     137 </span><span class="lineCov">       1150 :   luaE_enterCcall(L);</span>
<span class="lineNum">     138 </span><span class="lineCov">       1150 :   ci = luaM_new(L, CallInfo);</span>
<span class="lineNum">     139 </span>            :   lua_assert(L-&gt;ci-&gt;next == NULL);
<span class="lineNum">     140 </span><span class="lineCov">       1150 :   L-&gt;ci-&gt;next = ci;</span>
<span class="lineNum">     141 </span><span class="lineCov">       1150 :   ci-&gt;previous = L-&gt;ci;</span>
<span class="lineNum">     142 </span><span class="lineCov">       1150 :   ci-&gt;next = NULL;</span>
<span class="lineNum">     143 </span><span class="lineCov">       1150 :   ci-&gt;u.l.trap = 0;</span>
<span class="lineNum">     144 </span><span class="lineCov">       1150 :   L-&gt;nci++;</span>
<span class="lineNum">     145 </span><span class="lineCov">       1150 :   return ci;</span>
<span class="lineNum">     146 </span>            : }
<span class="lineNum">     147 </span>            : 
<span class="lineNum">     148 </span>            : 
<span class="lineNum">     149 </span>            : /*
<span class="lineNum">     150 </span>            : ** free all CallInfo structures not in use by a thread
<span class="lineNum">     151 </span>            : */
<span class="lineNum">     152 </span><span class="lineCov">        121 : void luaE_freeCI (lua_State *L) {</span>
<span class="lineNum">     153 </span><span class="lineCov">        121 :   CallInfo *ci = L-&gt;ci;</span>
<span class="lineNum">     154 </span><span class="lineCov">        121 :   CallInfo *next = ci-&gt;next;</span>
<span class="lineNum">     155 </span><span class="lineCov">        121 :   ci-&gt;next = NULL;</span>
<span class="lineNum">     156 </span><span class="lineCov">        121 :   L-&gt;nCcalls -= L-&gt;nci;  /* subtract removed elements from 'nCcalls' */</span>
<span class="lineNum">     157 </span><span class="lineCov">        711 :   while ((ci = next) != NULL) {</span>
<span class="lineNum">     158 </span><span class="lineCov">        590 :     next = ci-&gt;next;</span>
<span class="lineNum">     159 </span><span class="lineCov">        590 :     luaM_free(L, ci);</span>
<span class="lineNum">     160 </span><span class="lineCov">        590 :     L-&gt;nci--;</span>
<span class="lineNum">     161 </span>            :   }
<span class="lineNum">     162 </span><span class="lineCov">        121 :   L-&gt;nCcalls += L-&gt;nci;  /* adjust result */</span>
<span class="lineNum">     163 </span><span class="lineCov">        121 : }</span>
<span class="lineNum">     164 </span>            : 
<span class="lineNum">     165 </span>            : 
<span class="lineNum">     166 </span>            : /*
<span class="lineNum">     167 </span>            : ** free half of the CallInfo structures not in use by a thread
<span class="lineNum">     168 </span>            : */
<span class="lineNum">     169 </span><span class="lineCov">        813 : void luaE_shrinkCI (lua_State *L) {</span>
<span class="lineNum">     170 </span><span class="lineCov">        813 :   CallInfo *ci = L-&gt;ci;</span>
<span class="lineNum">     171 </span>            :   CallInfo *next2;  /* next's next */
<span class="lineNum">     172 </span><span class="lineCov">        813 :   L-&gt;nCcalls -= L-&gt;nci;  /* subtract removed elements from 'nCcalls' */</span>
<span class="lineNum">     173 </span>            :   /* while there are two nexts */
<span class="lineNum">     174 </span><span class="lineCov">       1326 :   while (ci-&gt;next != NULL &amp;&amp; (next2 = ci-&gt;next-&gt;next) != NULL) {</span>
<span class="lineNum">     175 </span><span class="lineCov">        513 :     luaM_free(L, ci-&gt;next);  /* free next */</span>
<span class="lineNum">     176 </span><span class="lineCov">        513 :     L-&gt;nci--;</span>
<span class="lineNum">     177 </span><span class="lineCov">        513 :     ci-&gt;next = next2;  /* remove 'next' from the list */</span>
<span class="lineNum">     178 </span><span class="lineCov">        513 :     next2-&gt;previous = ci;</span>
<span class="lineNum">     179 </span><span class="lineCov">        513 :     ci = next2;  /* keep next's next */</span>
<span class="lineNum">     180 </span>            :   }
<span class="lineNum">     181 </span><span class="lineCov">        813 :   L-&gt;nCcalls += L-&gt;nci;  /* adjust result */</span>
<span class="lineNum">     182 </span><span class="lineCov">        813 : }</span>
<span class="lineNum">     183 </span>            : 
<span class="lineNum">     184 </span>            : 
<span class="lineNum">     185 </span><span class="lineCov">        135 : static void stack_init (lua_State *L1, lua_State *L) {</span>
<span class="lineNum">     186 </span>            :   int i; CallInfo *ci;
<span class="lineNum">     187 </span>            :   /* initialize stack array */
<span class="lineNum">     188 </span><span class="lineCov">        135 :   L1-&gt;stack = luaM_newvector(L, BASIC_STACK_SIZE, StackValue);</span>
<span class="lineNum">     189 </span><span class="lineCov">        135 :   L1-&gt;stacksize = BASIC_STACK_SIZE;</span>
<span class="lineNum">     190 </span><span class="lineCov">       5535 :   for (i = 0; i &lt; BASIC_STACK_SIZE; i++)</span>
<span class="lineNum">     191 </span><span class="lineCov">       5400 :     setnilvalue(s2v(L1-&gt;stack + i));  /* erase new stack */</span>
<span class="lineNum">     192 </span><span class="lineCov">        135 :   L1-&gt;top = L1-&gt;stack;</span>
<span class="lineNum">     193 </span><span class="lineCov">        135 :   L1-&gt;stack_last = L1-&gt;stack + L1-&gt;stacksize - EXTRA_STACK;</span>
<span class="lineNum">     194 </span>            :   /* initialize first ci */
<span class="lineNum">     195 </span><span class="lineCov">        135 :   ci = &amp;L1-&gt;base_ci;</span>
<span class="lineNum">     196 </span><span class="lineCov">        135 :   ci-&gt;next = ci-&gt;previous = NULL;</span>
<span class="lineNum">     197 </span><span class="lineCov">        135 :   ci-&gt;callstatus = CIST_C;</span>
<span class="lineNum">     198 </span><span class="lineCov">        135 :   ci-&gt;func = L1-&gt;top;</span>
<span class="lineNum">     199 </span><span class="lineCov">        135 :   ci-&gt;u.c.k = NULL;</span>
<span class="lineNum">     200 </span><span class="lineCov">        135 :   ci-&gt;nresults = 0;</span>
<span class="lineNum">     201 </span><span class="lineCov">        135 :   setnilvalue(s2v(L1-&gt;top));  /* 'function' entry for this 'ci' */</span>
<span class="lineNum">     202 </span><span class="lineCov">        135 :   L1-&gt;top++;</span>
<span class="lineNum">     203 </span><span class="lineCov">        135 :   ci-&gt;top = L1-&gt;top + LUA_MINSTACK;</span>
<span class="lineNum">     204 </span><span class="lineCov">        135 :   L1-&gt;ci = ci;</span>
<span class="lineNum">     205 </span><span class="lineCov">        135 : }</span>
<span class="lineNum">     206 </span>            : 
<span class="lineNum">     207 </span>            : 
<span class="lineNum">     208 </span><span class="lineCov">        121 : static void freestack (lua_State *L) {</span>
<span class="lineNum">     209 </span><span class="lineCov">        121 :   if (L-&gt;stack == NULL)</span>
<span class="lineNum">     210 </span><span class="lineNoCov">          0 :     return;  /* stack not completely built yet */</span>
<span class="lineNum">     211 </span><span class="lineCov">        121 :   L-&gt;ci = &amp;L-&gt;base_ci;  /* free the entire 'ci' list */</span>
<span class="lineNum">     212 </span><span class="lineCov">        121 :   luaE_freeCI(L);</span>
<span class="lineNum">     213 </span>            :   lua_assert(L-&gt;nci == 0);
<span class="lineNum">     214 </span><span class="lineCov">        121 :   luaM_freearray(L, L-&gt;stack, L-&gt;stacksize);  /* free stack array */</span>
<span class="lineNum">     215 </span>            : }
<span class="lineNum">     216 </span>            : 
<span class="lineNum">     217 </span>            : 
<span class="lineNum">     218 </span>            : /*
<span class="lineNum">     219 </span>            : ** Create registry table and its predefined values
<span class="lineNum">     220 </span>            : */
<span class="lineNum">     221 </span><span class="lineCov">        108 : static void init_registry (lua_State *L, global_State *g) {</span>
<span class="lineNum">     222 </span>            :   TValue temp;
<span class="lineNum">     223 </span>            :   /* create registry */
<span class="lineNum">     224 </span><span class="lineCov">        108 :   Table *registry = luaH_new(L);</span>
<span class="lineNum">     225 </span><span class="lineCov">        108 :   sethvalue(L, &amp;g-&gt;l_registry, registry);</span>
<span class="lineNum">     226 </span><span class="lineCov">        108 :   luaH_resize(L, registry, LUA_RIDX_LAST, 0);</span>
<span class="lineNum">     227 </span>            :   /* registry[LUA_RIDX_MAINTHREAD] = L */
<span class="lineNum">     228 </span><span class="lineCov">        108 :   setthvalue(L, &amp;temp, L);  /* temp = L */</span>
<span class="lineNum">     229 </span><span class="lineCov">        108 :   luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &amp;temp);</span>
<span class="lineNum">     230 </span>            :   /* registry[LUA_RIDX_GLOBALS] = table of globals */
<span class="lineNum">     231 </span><span class="lineCov">        108 :   sethvalue(L, &amp;temp, luaH_new(L));  /* temp = new table (global table) */</span>
<span class="lineNum">     232 </span><span class="lineCov">        108 :   luaH_setint(L, registry, LUA_RIDX_GLOBALS, &amp;temp);</span>
<span class="lineNum">     233 </span><span class="lineCov">        108 : }</span>
<span class="lineNum">     234 </span>            : 
<span class="lineNum">     235 </span>            : 
<span class="lineNum">     236 </span>            : /*
<span class="lineNum">     237 </span>            : ** open parts of the state that may cause memory-allocation errors.
<span class="lineNum">     238 </span>            : ** ('g-&gt;nilvalue' being a nil value flags that the state was completely
<span class="lineNum">     239 </span>            : ** build.)
<span class="lineNum">     240 </span>            : */
<span class="lineNum">     241 </span><span class="lineCov">        108 : static void f_luaopen (lua_State *L, void *ud) {</span>
<span class="lineNum">     242 </span><span class="lineCov">        108 :   global_State *g = G(L);</span>
<span class="lineNum">     243 </span>            :   UNUSED(ud);
<span class="lineNum">     244 </span><span class="lineCov">        108 :   stack_init(L, L);  /* init stack */</span>
<span class="lineNum">     245 </span><span class="lineCov">        108 :   init_registry(L, g);</span>
<span class="lineNum">     246 </span><span class="lineCov">        108 :   luaS_init(L);</span>
<span class="lineNum">     247 </span><span class="lineCov">        108 :   luaT_init(L);</span>
<span class="lineNum">     248 </span><span class="lineCov">        108 :   luaX_init(L);</span>
<span class="lineNum">     249 </span><span class="lineCov">        108 :   g-&gt;gcrunning = 1;  /* allow gc */</span>
<span class="lineNum">     250 </span><span class="lineCov">        108 :   setnilvalue(&amp;g-&gt;nilvalue);</span>
<span class="lineNum">     251 </span>            :   luai_userstateopen(L);
<span class="lineNum">     252 </span><span class="lineCov">        108 : }</span>
<span class="lineNum">     253 </span>            : 
<span class="lineNum">     254 </span>            : 
<span class="lineNum">     255 </span>            : /*
<span class="lineNum">     256 </span>            : ** preinitialize a thread with consistent values without allocating
<span class="lineNum">     257 </span>            : ** any memory (to avoid errors)
<span class="lineNum">     258 </span>            : */
<span class="lineNum">     259 </span><span class="lineCov">        135 : static void preinit_thread (lua_State *L, global_State *g) {</span>
<span class="lineNum">     260 </span><span class="lineCov">        135 :   G(L) = g;</span>
<span class="lineNum">     261 </span><span class="lineCov">        135 :   L-&gt;stack = NULL;</span>
<span class="lineNum">     262 </span><span class="lineCov">        135 :   L-&gt;ci = NULL;</span>
<span class="lineNum">     263 </span><span class="lineCov">        135 :   L-&gt;nci = 0;</span>
<span class="lineNum">     264 </span><span class="lineCov">        135 :   L-&gt;stacksize = 0;</span>
<span class="lineNum">     265 </span><span class="lineCov">        135 :   L-&gt;twups = L;  /* thread has no upvalues */</span>
<span class="lineNum">     266 </span><span class="lineCov">        135 :   L-&gt;errorJmp = NULL;</span>
<span class="lineNum">     267 </span><span class="lineCov">        135 :   L-&gt;nCcalls = 0;</span>
<span class="lineNum">     268 </span><span class="lineCov">        135 :   L-&gt;hook = NULL;</span>
<span class="lineNum">     269 </span><span class="lineCov">        135 :   L-&gt;hookmask = 0;</span>
<span class="lineNum">     270 </span><span class="lineCov">        135 :   L-&gt;basehookcount = 0;</span>
<span class="lineNum">     271 </span><span class="lineCov">        135 :   L-&gt;allowhook = 1;</span>
<span class="lineNum">     272 </span><span class="lineCov">        135 :   resethookcount(L);</span>
<span class="lineNum">     273 </span><span class="lineCov">        135 :   L-&gt;openupval = NULL;</span>
<span class="lineNum">     274 </span><span class="lineCov">        135 :   L-&gt;status = LUA_OK;</span>
<span class="lineNum">     275 </span><span class="lineCov">        135 :   L-&gt;errfunc = 0;</span>
<span class="lineNum">     276 </span><span class="lineCov">        135 : }</span>
<span class="lineNum">     277 </span>            : 
<span class="lineNum">     278 </span>            : 
<span class="lineNum">     279 </span><span class="lineCov">         94 : static void close_state (lua_State *L) {</span>
<span class="lineNum">     280 </span><span class="lineCov">         94 :   global_State *g = G(L);</span>
<span class="lineNum">     281 </span><span class="lineCov">         94 :   luaF_close(L, L-&gt;stack, CLOSEPROTECT);  /* close all upvalues */</span>
<span class="lineNum">     282 </span><span class="lineCov">         94 :   luaC_freeallobjects(L);  /* collect all objects */</span>
<span class="lineNum">     283 </span><span class="lineCov">         94 :   if (ttisnil(&amp;g-&gt;nilvalue))  /* closing a fully built state? */</span>
<span class="lineNum">     284 </span>            :     luai_userstateclose(L);
<span class="lineNum">     285 </span><span class="lineCov">         94 :   luaM_freearray(L, G(L)-&gt;strt.hash, G(L)-&gt;strt.size);</span>
<span class="lineNum">     286 </span><span class="lineCov">         94 :   freestack(L);</span>
<span class="lineNum">     287 </span>            :   lua_assert(gettotalbytes(g) == sizeof(LG));
<span class="lineNum">     288 </span><span class="lineCov">         94 :   (*g-&gt;frealloc)(g-&gt;ud, fromstate(L), sizeof(LG), 0);  /* free main block */</span>
<span class="lineNum">     289 </span><span class="lineCov">         94 : }</span>
<span class="lineNum">     290 </span>            : 
<span class="lineNum">     291 </span>            : 
<span class="lineNum">     292 </span><span class="lineCov">         27 : LUA_API lua_State *lua_newthread (lua_State *L) {</span>
<span class="lineNum">     293 </span><span class="lineCov">         27 :   global_State *g = G(L);</span>
<span class="lineNum">     294 </span>            :   lua_State *L1;
<span class="lineNum">     295 </span>            :   lua_lock(L);
<span class="lineNum">     296 </span><span class="lineCov">         27 :   luaC_checkGC(L);</span>
<span class="lineNum">     297 </span>            :   /* create new thread */
<span class="lineNum">     298 </span><span class="lineCov">         27 :   L1 = &amp;cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))-&gt;l;</span>
<span class="lineNum">     299 </span><span class="lineCov">         27 :   L1-&gt;marked = luaC_white(g);</span>
<span class="lineNum">     300 </span><span class="lineCov">         27 :   L1-&gt;tt = LUA_TTHREAD;</span>
<span class="lineNum">     301 </span>            :   /* link it on list 'allgc' */
<span class="lineNum">     302 </span><span class="lineCov">         27 :   L1-&gt;next = g-&gt;allgc;</span>
<span class="lineNum">     303 </span><span class="lineCov">         27 :   g-&gt;allgc = obj2gco(L1);</span>
<span class="lineNum">     304 </span>            :   /* anchor it on L stack */
<span class="lineNum">     305 </span><span class="lineCov">         27 :   setthvalue2s(L, L-&gt;top, L1);</span>
<span class="lineNum">     306 </span><span class="lineCov">         27 :   api_incr_top(L);</span>
<span class="lineNum">     307 </span><span class="lineCov">         27 :   preinit_thread(L1, g);</span>
<span class="lineNum">     308 </span><span class="lineCov">         27 :   L1-&gt;hookmask = L-&gt;hookmask;</span>
<span class="lineNum">     309 </span><span class="lineCov">         27 :   L1-&gt;basehookcount = L-&gt;basehookcount;</span>
<span class="lineNum">     310 </span><span class="lineCov">         27 :   L1-&gt;hook = L-&gt;hook;</span>
<span class="lineNum">     311 </span><span class="lineCov">         27 :   resethookcount(L1);</span>
<span class="lineNum">     312 </span>            :   /* initialize L1 extra space */
<span class="lineNum">     313 </span><span class="lineCov">         27 :   memcpy(lua_getextraspace(L1), lua_getextraspace(g-&gt;mainthread),</span>
<span class="lineNum">     314 </span>            :          LUA_EXTRASPACE);
<span class="lineNum">     315 </span>            :   luai_userstatethread(L, L1);
<span class="lineNum">     316 </span><span class="lineCov">         27 :   stack_init(L1, L);  /* init stack */</span>
<span class="lineNum">     317 </span>            :   lua_unlock(L);
<span class="lineNum">     318 </span><span class="lineCov">         27 :   return L1;</span>
<span class="lineNum">     319 </span>            : }
<span class="lineNum">     320 </span>            : 
<span class="lineNum">     321 </span>            : 
<span class="lineNum">     322 </span><span class="lineCov">         27 : void luaE_freethread (lua_State *L, lua_State *L1) {</span>
<span class="lineNum">     323 </span><span class="lineCov">         27 :   LX *l = fromstate(L1);</span>
<span class="lineNum">     324 </span><span class="lineCov">         27 :   luaF_close(L1, L1-&gt;stack, NOCLOSINGMETH);  /* close all upvalues */</span>
<span class="lineNum">     325 </span>            :   lua_assert(L1-&gt;openupval == NULL);
<span class="lineNum">     326 </span>            :   luai_userstatefree(L, L1);
<span class="lineNum">     327 </span><span class="lineCov">         27 :   freestack(L1);</span>
<span class="lineNum">     328 </span><span class="lineCov">         27 :   luaM_free(L, l);</span>
<span class="lineNum">     329 </span><span class="lineCov">         27 : }</span>
<span class="lineNum">     330 </span>            : 
<span class="lineNum">     331 </span>            : 
<span class="lineNum">     332 </span><span class="lineCov">          3 : int lua_resetthread (lua_State *L) {</span>
<span class="lineNum">     333 </span>            :   CallInfo *ci;
<span class="lineNum">     334 </span>            :   int status;
<span class="lineNum">     335 </span>            :   lua_lock(L);
<span class="lineNum">     336 </span><span class="lineCov">          3 :   ci = &amp;L-&gt;base_ci;</span>
<span class="lineNum">     337 </span><span class="lineCov">          3 :   status = luaF_close(L, L-&gt;stack, CLOSEPROTECT);</span>
<span class="lineNum">     338 </span><span class="lineCov">          3 :   setnilvalue(s2v(L-&gt;stack));  /* 'function' entry for basic 'ci' */</span>
<span class="lineNum">     339 </span><span class="lineCov">          3 :   if (status != CLOSEPROTECT)  /* real errors? */</span>
<span class="lineNum">     340 </span><span class="lineNoCov">          0 :     luaD_seterrorobj(L, status, L-&gt;stack + 1);</span>
<span class="lineNum">     341 </span>            :   else {
<span class="lineNum">     342 </span><span class="lineCov">          3 :     status = LUA_OK;</span>
<span class="lineNum">     343 </span><span class="lineCov">          3 :     L-&gt;top = L-&gt;stack + 1;</span>
<span class="lineNum">     344 </span>            :   }
<span class="lineNum">     345 </span><span class="lineCov">          3 :   ci-&gt;callstatus = CIST_C;</span>
<span class="lineNum">     346 </span><span class="lineCov">          3 :   ci-&gt;func = L-&gt;stack;</span>
<span class="lineNum">     347 </span><span class="lineCov">          3 :   ci-&gt;top = L-&gt;top + LUA_MINSTACK;</span>
<span class="lineNum">     348 </span><span class="lineCov">          3 :   L-&gt;ci = ci;</span>
<span class="lineNum">     349 </span><span class="lineCov">          3 :   L-&gt;status = status;</span>
<span class="lineNum">     350 </span>            :   lua_unlock(L);
<span class="lineNum">     351 </span><span class="lineCov">          3 :   return status;</span>
<span class="lineNum">     352 </span>            : }
<span class="lineNum">     353 </span>            : 
<span class="lineNum">     354 </span>            : 
<span class="lineNum">     355 </span><span class="lineCov">        108 : LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {</span>
<span class="lineNum">     356 </span>            :   int i;
<span class="lineNum">     357 </span>            :   lua_State *L;
<span class="lineNum">     358 </span>            :   global_State *g;
<span class="lineNum">     359 </span><span class="lineCov">        108 :   LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));</span>
<span class="lineNum">     360 </span><span class="lineCov">        108 :   if (l == NULL) return NULL;</span>
<span class="lineNum">     361 </span><span class="lineCov">        108 :   L = &amp;l-&gt;l.l;</span>
<span class="lineNum">     362 </span><span class="lineCov">        108 :   g = &amp;l-&gt;g;</span>
<span class="lineNum">     363 </span><span class="lineCov">        108 :   L-&gt;tt = LUA_TTHREAD;</span>
<span class="lineNum">     364 </span><span class="lineCov">        108 :   g-&gt;currentwhite = bitmask(WHITE0BIT);</span>
<span class="lineNum">     365 </span><span class="lineCov">        108 :   L-&gt;marked = luaC_white(g);</span>
<span class="lineNum">     366 </span><span class="lineCov">        108 :   preinit_thread(L, g);</span>
<span class="lineNum">     367 </span><span class="lineCov">        108 :   g-&gt;allgc = obj2gco(L);  /* by now, only object is the main thread */</span>
<span class="lineNum">     368 </span><span class="lineCov">        108 :   L-&gt;next = NULL;</span>
<span class="lineNum">     369 </span><span class="lineCov">        108 :   g-&gt;frealloc = f;</span>
<span class="lineNum">     370 </span><span class="lineCov">        108 :   g-&gt;ud = ud;</span>
<span class="lineNum">     371 </span><span class="lineCov">        108 :   g-&gt;warnf = NULL;</span>
<span class="lineNum">     372 </span><span class="lineCov">        108 :   g-&gt;ud_warn = NULL;</span>
<span class="lineNum">     373 </span><span class="lineCov">        108 :   g-&gt;mainthread = L;</span>
<span class="lineNum">     374 </span><span class="lineCov">        108 :   g-&gt;seed = luai_makeseed(L);</span>
<span class="lineNum">     375 </span><span class="lineCov">        108 :   g-&gt;gcrunning = 0;  /* no GC while building state */</span>
<span class="lineNum">     376 </span><span class="lineCov">        108 :   g-&gt;strt.size = g-&gt;strt.nuse = 0;</span>
<span class="lineNum">     377 </span><span class="lineCov">        108 :   g-&gt;strt.hash = NULL;</span>
<span class="lineNum">     378 </span><span class="lineCov">        108 :   setnilvalue(&amp;g-&gt;l_registry);</span>
<span class="lineNum">     379 </span><span class="lineCov">        108 :   g-&gt;panic = NULL;</span>
<span class="lineNum">     380 </span><span class="lineCov">        108 :   g-&gt;gcstate = GCSpause;</span>
<span class="lineNum">     381 </span><span class="lineCov">        108 :   g-&gt;gckind = KGC_INC;</span>
<span class="lineNum">     382 </span><span class="lineCov">        108 :   g-&gt;gcemergency = 0;</span>
<span class="lineNum">     383 </span><span class="lineCov">        108 :   g-&gt;finobj = g-&gt;tobefnz = g-&gt;fixedgc = NULL;</span>
<span class="lineNum">     384 </span><span class="lineCov">        108 :   g-&gt;survival = g-&gt;old = g-&gt;reallyold = NULL;</span>
<span class="lineNum">     385 </span><span class="lineCov">        108 :   g-&gt;finobjsur = g-&gt;finobjold = g-&gt;finobjrold = NULL;</span>
<span class="lineNum">     386 </span><span class="lineCov">        108 :   g-&gt;sweepgc = NULL;</span>
<span class="lineNum">     387 </span><span class="lineCov">        108 :   g-&gt;gray = g-&gt;grayagain = NULL;</span>
<span class="lineNum">     388 </span><span class="lineCov">        108 :   g-&gt;weak = g-&gt;ephemeron = g-&gt;allweak = NULL;</span>
<span class="lineNum">     389 </span><span class="lineCov">        108 :   g-&gt;twups = NULL;</span>
<span class="lineNum">     390 </span><span class="lineCov">        108 :   g-&gt;totalbytes = sizeof(LG);</span>
<span class="lineNum">     391 </span><span class="lineCov">        108 :   g-&gt;GCdebt = 0;</span>
<span class="lineNum">     392 </span><span class="lineCov">        108 :   g-&gt;lastatomic = 0;</span>
<span class="lineNum">     393 </span><span class="lineCov">        108 :   setivalue(&amp;g-&gt;nilvalue, 0);  /* to signal that state is not yet built */</span>
<span class="lineNum">     394 </span><span class="lineCov">        108 :   setgcparam(g-&gt;gcpause, LUAI_GCPAUSE);</span>
<span class="lineNum">     395 </span><span class="lineCov">        108 :   setgcparam(g-&gt;gcstepmul, LUAI_GCMUL);</span>
<span class="lineNum">     396 </span><span class="lineCov">        108 :   g-&gt;gcstepsize = LUAI_GCSTEPSIZE;</span>
<span class="lineNum">     397 </span><span class="lineCov">        108 :   setgcparam(g-&gt;genmajormul, LUAI_GENMAJORMUL);</span>
<span class="lineNum">     398 </span><span class="lineCov">        108 :   g-&gt;genminormul = LUAI_GENMINORMUL;</span>
<span class="lineNum">     399 </span><span class="lineCov">       1080 :   for (i=0; i &lt; LUA_NUMTAGS; i++) g-&gt;mt[i] = NULL;</span>
<span class="lineNum">     400 </span><span class="lineCov">        108 :   if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {</span>
<span class="lineNum">     401 </span>            :     /* memory allocation error: free partial state */
<span class="lineNum">     402 </span><span class="lineNoCov">          0 :     close_state(L);</span>
<span class="lineNum">     403 </span><span class="lineNoCov">          0 :     L = NULL;</span>
<span class="lineNum">     404 </span>            :   }
<span class="lineNum">     405 </span><span class="lineCov">        108 :   return L;</span>
<span class="lineNum">     406 </span>            : }
<span class="lineNum">     407 </span>            : 
<span class="lineNum">     408 </span>            : 
<span class="lineNum">     409 </span><span class="lineCov">         94 : LUA_API void lua_close (lua_State *L) {</span>
<span class="lineNum">     410 </span><span class="lineCov">         94 :   L = G(L)-&gt;mainthread;  /* only the main thread can be closed */</span>
<span class="lineNum">     411 </span>            :   lua_lock(L);
<span class="lineNum">     412 </span><span class="lineCov">         94 :   close_state(L);</span>
<span class="lineNum">     413 </span><span class="lineCov">         94 : }</span>
<span class="lineNum">     414 </span>            : 
<span class="lineNum">     415 </span>            : 
<span class="lineNum">     416 </span><span class="lineCov">          7 : void luaE_warning (lua_State *L, const char *msg, int tocont) {</span>
<span class="lineNum">     417 </span><span class="lineCov">          7 :   lua_WarnFunction wf = G(L)-&gt;warnf;</span>
<span class="lineNum">     418 </span><span class="lineCov">          7 :   if (wf != NULL)</span>
<span class="lineNum">     419 </span><span class="lineCov">          7 :     wf(G(L)-&gt;ud_warn, msg, tocont);</span>
<span class="lineNum">     420 </span><span class="lineCov">          7 : }</span>
<span class="lineNum">     421 </span>            : 
<span class="lineNum">     422 </span>            : 
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.13</a></td></tr>
  </table>
  <br>

</body>
</html>
